//
//  LeftMenuViewController.m
//  suRead
//
//  Created by lidi on 15/11/6.
//  Copyright © 2015年 bao. All rights reserved.
//

#import "LeftMenuViewController.h"
#import "Masonry.h"
#import "UIControl+Blocks.h"
#import "AccountManager.h"
#import "VerticelAlignButton.h"
#import "UIAlertView+Blocks.h"


#import "AppDelegate.h"
#import "UIApplication+VisibleViewController.h"

#import "PocketNewspapersViewController.h"
#import "ServingViewController.h"
#import "InteractiveViewController.h"
#import "QRViewController.h"
#import "LoginViewController.h"
#import "MyCommentViewController.h"
#import "MyMessageViewController.h"
#import "UIImageView+WebCache.h"

#import "PersonalSetUpContainerViewController.h"
#import "PublishFeedBackViewController.h"

#import "MyShareRecommendViewController.h"
#import "MyWelfareViewController.h"
#import "SearchViewController.h"
#import "PersonalEditInformationViewController.h"

#import "HelpViewController.h"

#import "ServiceUser.h"
#import "ModalViewController.h"

@interface LeftMenuViewController ()
@property (weak, nonatomic) IBOutlet UIView *menuContainerView;

@property (weak, nonatomic) IBOutlet UIImageView *imageViewIcon;
@property (weak, nonatomic) IBOutlet UILabel *labelUserName;

@property (weak, nonatomic) IBOutlet UILabel *scroeLable;
@property (weak, nonatomic) IBOutlet UILabel *LevelLable;

@property (weak, nonatomic) IBOutlet UIView *searchView;


@end

@implementation LeftMenuViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.contentViewControllerHandle = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if ([AccountManager sharedAccountManager].f_isLogined) {
        [ServiceUser doGetUserInfo:[AccountManager sharedAccountManager].f_uid
                           success:^(id responseObject, NSString *message)
         {
             self.labelUserName.text = [AccountManager sharedAccountManager].f_nickname;
             [self.imageViewIcon sd_setImageWithURL:[NSURL URLWithString:[AccountManager sharedAccountManager].f_user_img] placeholderImage:[UIImage imageNamed:@"icon_personal"]];
             
             [self.scroeLable setText:[NSString stringWithFormat:@"积分 %@",[AccountManager sharedAccountManager].f_point]];
             [self.LevelLable setText:[AccountManager sharedAccountManager].f_member_level];
             
         }
                           failure:^(NSString *message)
         {
         }];
        
        self.labelUserName.text = [AccountManager sharedAccountManager].f_nickname;
        [self.imageViewIcon sd_setImageWithURL:[NSURL URLWithString:[AccountManager sharedAccountManager].f_user_img] placeholderImage:[UIImage imageNamed:@"icon_personal"]];
        
        
        [self.scroeLable setText:[NSString stringWithFormat:@"积分 %@",[AccountManager sharedAccountManager].f_point]];
        [self.LevelLable setText:[AccountManager sharedAccountManager].f_member_level];

    }
    else
    {
        self.labelUserName.text = @"立即登录";
        self.imageViewIcon.image = [UIImage imageNamed:@"icon_personal"];

        [self.scroeLable setText:@"积分"];
        [self.LevelLable setText:@""];
    }
    
    NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
    [nc addObserver:self
           selector:@selector(handleColorChange:)
               name:@"do"
             object:nil];
    
}
-(void)handleColorChange:(id)sender{
    self.labelUserName.text = @"立即登录";
    self.imageViewIcon.image = [UIImage imageNamed:@"icon_personal"];
    
    [self.scroeLable setText:@"积分"];
    [self.LevelLable setText:@""];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blueColor];
    self.navigationController.navigationBarHidden = YES;
    
    self.searchView.layer.cornerRadius = 5.0f;
    self.searchView.layer.masksToBounds = YES;

    self.imageViewIcon.layer.cornerRadius = 25.0f;
    self.imageViewIcon.layer.masksToBounds = YES;
    
    // Do any additional setup after loading the view.
    NSArray *menusTitle = @[@"  我的评论", @"  我的消息", @"  我的福利", @"  分享推荐"];
    NSArray *menuImages = @[@"my_comment_icon",@"my_message_icon",@"my_welfare_icon",@"my_share_icon"];
    
    UIView *preView = nil;
    for (NSInteger i=0; i<[menusTitle count]; i++) {
        
        UIButton *buttonMenu = [UIButton buttonWithType:UIButtonTypeCustom];        
        buttonMenu.tag = i;
        
        [buttonMenu addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
            switch (sender.tag) {
                case 0:
                {
                    //我的评论
                    if (![AccountManager sharedAccountManager].f_isLogined) {
                        [UIAlertView showWithTitle:@"提示" message:@"请先登录，领取福利须绑定手机号" cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                            if (buttonIndex == 1)
                            {

                        
                        //2016-6-22 hold left menu
                        [AppDelegate sharedAppDelegate].myOpen = @"1";
                        //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
                        
                        UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
                        LoginViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"LoginViewController"];
                        controller.hidesBottomBarWhenPushed = YES;
                        [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
                                
                            }
                        }];
                        return;
                    }
                    else{
                        //2016-6-22 hold left menu
                        [AppDelegate sharedAppDelegate].myOpen = @"1";
                        //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
                        
                        MyCommentViewController *controller = [[MyCommentViewController alloc] init];
                        controller.hidesBottomBarWhenPushed = YES;
                        [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
                    }
                }
                    break;
                case 1:
                {
                    
                    
                    //我的消息
                    if (![AccountManager sharedAccountManager].f_isLogined) {
                        //请绑定手机号 请先登录，领取福利须绑定手机号
                        [UIAlertView showWithTitle:@"提示" message:@"请先登录，领取福利须绑定手机号" cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                            if (buttonIndex == 1)
                            {
                        
                        //2016-6-22 hold left menu
                        [AppDelegate sharedAppDelegate].myOpen = @"1";
                        //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
                        
                        UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
                        LoginViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"LoginViewController"];
                        controller.hidesBottomBarWhenPushed = YES;
                        [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
                            }}];
                    }
                    else{
                        //2016-6-22 hold left menu
                        [AppDelegate sharedAppDelegate].myOpen = @"1";
                        //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
                        
                        MyMessageViewController *controller = [[MyMessageViewController alloc] init];
                        controller.hidesBottomBarWhenPushed = YES;
                        [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
                    }
                }
                    break;
                case 2:
                {
                    
                    if (![AccountManager sharedAccountManager].f_isLogined) {
                        
                        //请绑定手机号 请先登录，领取福利须绑定手机号
                        [UIAlertView showWithTitle:@"提示" message:@"请先登录，领取福利须绑定手机号" cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                            if (buttonIndex == 1)
                            {
                                //2016-6-22 hold left menu
                                [AppDelegate sharedAppDelegate].myOpen = @"1";
                                //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
                                
                                UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
                                LoginViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"LoginViewController"];
                                controller.hidesBottomBarWhenPushed = YES;
                                [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
                                
                            }
                        }];
                        
                        return;
                        
                    }
                    
                    if (![AccountManager sharedAccountManager].f_phone_num || [[AccountManager sharedAccountManager].f_phone_num isEqualToString:@""]) {
                        //请绑定手机号 请先登录，领取福利须绑定手机号
                        [UIAlertView showWithTitle:@"提示" message:@"请绑定手机号" cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                            if (buttonIndex == 1)
                            {
                                //2016-6-22 hold left menu
                                [AppDelegate sharedAppDelegate].myOpen = @"1";
                                //绑定手机号
                                //                                [[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
                                //
                                //                                UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
                                //                                PersonalEditInformationViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"PersonalEditInformationViewController"];
                                //                                controller.hidesBottomBarWhenPushed = YES;
                                //
                                //                                [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
                                
                                //显示模态窗体
                                ModalViewController *mvc =  [[ModalViewController alloc] init];
                                mvc.delegate = self;
                                [mvc initStyle];
                                
                                [[[UIApplication sharedApplication] visibleViewController].navigationController presentViewController:mvc animated:YES completion:^(void){
                                    mvc.view.superview.backgroundColor = [UIColor clearColor];
                                    
                                }];
                                
                            }
                        }];
                        
                        return;
                    }
                    
                    //2016-6-22 hold left menu
                    [AppDelegate sharedAppDelegate].myOpen = @"1";
                    //我的福利
                    //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
                    
                    UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
                    MyWelfareViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"MyWelfareViewController"];
                    controller.hidesBottomBarWhenPushed = YES;
                    [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
                }
                    break;
                    case 3:
                {
                    
                    if (![AccountManager sharedAccountManager].f_isLogined) {
                        //请绑定手机号 请先登录，领取福利须绑定手机号
                        [UIAlertView showWithTitle:@"提示" message:@"请先登录，领取福利须绑定手机号" cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                            if (buttonIndex == 1)
                            {
                        
                        
                        //2016-6-22 hold left menu
                        [AppDelegate sharedAppDelegate].myOpen = @"1";
                        //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
                        
                        UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
                        LoginViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"LoginViewController"];
                        controller.hidesBottomBarWhenPushed = YES;
                        [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
                            }
                        }];
                        return;
                    }
                    
                    if (![AccountManager sharedAccountManager].f_phone_num || [[AccountManager sharedAccountManager].f_phone_num isEqualToString:@""]) {
                        [UIAlertView showWithTitle:@"提示" message:@"请绑定手机号" cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                            if (buttonIndex == 1)
                            {
                                //2016-6-22 hold left menu
                                [AppDelegate sharedAppDelegate].myOpen = @"1";
                                //绑定手机号
//                                [[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
//                                
//                                UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
//                                PersonalEditInformationViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"PersonalEditInformationViewController"];
//                                controller.hidesBottomBarWhenPushed = YES;
//                                controller.title=@"个人信息";
//                                
//                                [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
//                                
                                //显示模态窗体
                                ModalViewController *mvc =  [[ModalViewController alloc] init];
                                mvc.delegate = self;
                                [mvc initStyle];
                                
                                [[[UIApplication sharedApplication] visibleViewController].navigationController presentViewController:mvc animated:YES completion:^(void){
                                    mvc.view.superview.backgroundColor = [UIColor clearColor];
                                    
                                }];

                            }
                        }];
                        
                        return;
                    }
                    
                    //分享推荐
                    //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
                    
                    //2016-6-22 hold left menu
                    [AppDelegate sharedAppDelegate].myOpen = @"1";

                    UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
                    MyShareRecommendViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"MyShareRecommendViewController"];
                    controller.hidesBottomBarWhenPushed = YES;
                    [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
                }
                    break;
                    case 4:
                {
                    //2016-6-22 hold left menu
                    [AppDelegate sharedAppDelegate].myOpen = @"1";
                    //互动
                    UIViewController *interactiveViewController = [self.contentViewControllerHandle objectForKey:kInteractiveViewController];
                    if (!interactiveViewController) {
                        interactiveViewController = [[UINavigationController alloc] initWithRootViewController: [[InteractiveViewController alloc] initWithNibName:@"InteractiveViewController" bundle:[NSBundle mainBundle]]];
                        [self.contentViewControllerHandle setObject:interactiveViewController forKey:kInteractiveViewController];
                    }
                    
                    //[[AppDelegate sharedAppDelegate].sideMenuViewController setContentViewController:interactiveViewController animated:YES];
                    //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
                }
                    break;
                case 5:
                {
                    //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
                    QRViewController *controller = [QRViewController new];
                    //[(UINavigationController *)[AppDelegate sharedAppDelegate].sideMenuViewController.contentViewController pushViewController:controller animated:YES];
                }
                    break;
                default:
                    break;
            }
        }];
        
        NSString *title = [menusTitle objectAtIndex:i];
        NSString *image = [menuImages objectAtIndex:i];
        
        [buttonMenu setTitle:title forState:UIControlStateNormal];
        buttonMenu.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        [buttonMenu setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
        
        [self.menuContainerView addSubview:buttonMenu];
        
        [buttonMenu mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(buttonMenu.superview);
            if (!preView) {
                make.top.equalTo(buttonMenu.superview).with.offset(50.0f);
            }
            else {
                make.top.equalTo(preView.mas_bottom);
                make.height.mas_equalTo(preView.mas_height);
            }
            
            if (i==[menusTitle count] - 1) {
                make.bottom.equalTo(buttonMenu.superview).with.offset(-50.0f);
            }
        }];
        
        preView = buttonMenu;
        
    }
    
}

/**
 *  设置
 *
 *  @param sender
 */
- (IBAction)setting:(id)sender
{
    //2016-6-22 hold left menu
    [AppDelegate sharedAppDelegate].myOpen = @"1";

    //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
    
    UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
    PersonalSetUpContainerViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"PersonalSetUpContainerViewController"];
    controller.hidesBottomBarWhenPushed = YES;

    [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
    
}

/**
 *  登录
 *
 *  @param sender
 */
- (IBAction)login:(id)sender
{
    if (![AccountManager sharedAccountManager].f_isLogined) {
        [AppDelegate sharedAppDelegate].myOpen = @"1";
        //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
        
        UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
        LoginViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"LoginViewController"];
        controller.hidesBottomBarWhenPushed = YES;

        [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
    }
    else
    {
        //2016-6-22 hold left menu
        [AppDelegate sharedAppDelegate].myOpen = @"1";
        //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
        
        UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
        PersonalEditInformationViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"PersonalEditInformationViewController"];
        controller.hidesBottomBarWhenPushed = YES;

        [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
      
    }
}


/**
 *  反馈
 *
 *  @param sender
 */
- (IBAction)feedback:(id)sender
{
    [AppDelegate sharedAppDelegate].myOpen = @"1";
    //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
    
    UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
    PublishFeedBackViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"PublishFeedBackViewController"];
    controller.hidesBottomBarWhenPushed = YES;
    [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
}

/**
 *  帮助
 *
 *  @param sender
 */
- (IBAction)notification:(id)sender
{
    [AppDelegate sharedAppDelegate].myOpen = @"1";
    //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];
    
    UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
    HelpViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"HelpViewController"];
    controller.hidesBottomBarWhenPushed = YES;
    [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
}


- (IBAction)search:(id)sender
{
    [AppDelegate sharedAppDelegate].myOpen = @"1";
    //[[AppDelegate sharedAppDelegate].sideMenuViewController hideMenuViewController];

    UIStoryboard *personalCenterStoryBoard = [UIStoryboard storyboardWithName:@"PersonalCenter" bundle:[NSBundle mainBundle]];
    SearchViewController *controller = [personalCenterStoryBoard instantiateViewControllerWithIdentifier:@"SearchViewController"];
    controller.hidesBottomBarWhenPushed = YES;
    [[[UIApplication sharedApplication] visibleViewController].navigationController pushViewController:controller animated:YES];
}

@end
